package com.example.mall.order.domain.model;

import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.example.mall.common.ddd.AbstractEntity;
import com.example.mall.order.domain.constants.OrderType;
import com.example.mall.order.domain.service.OrderSettle;
import com.example.mall.order.domain.vo.FeeItem;
import com.example.mall.order.domain.vo.MerchItem;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author Mickey
 * @since 2022/4/22
 **/
@EqualsAndHashCode(callSuper = true)
@Data
@TableName(value = "pm_order", autoResultMap = true)
public class Order extends AbstractEntity {

    private Long userId;
    private OrderType type;
    private Integer status;
    private Integer payMode;
    private BigDecimal totalFee;
    @TableField(typeHandler = FeeItem.FeeItemArrayType.class)
    private List<FeeItem> feeItems;
    @TableField(typeHandler = MerchItem.MerchItemArrayType.class)
    private List<MerchItem> merchItems;

    public void calculate() {
        BigDecimal merchFee = this.merchItems
                .stream()
                .reduce(BigDecimal.ZERO, (a, b) -> a.add(b.getPrice()), (a, b) -> null);
        this.feeItems.add(new FeeItem(1, merchFee));

        this.totalFee = this.feeItems
                .stream()
                .reduce(BigDecimal.ZERO, (a, b) -> a.add(b.getFee()), (a, b) -> null);
        OrderSettle orderSettle = SpringUtil.getBean(type.getName());
        orderSettle.calc(this.merchItems);

    }
}
